package com.hk.dao.impl;

import com.hk.dao.INewsTypeDao;
import com.hk.model.NewsType;
import com.hk.utils.JdbcMysql;
import com.hk.utils.SqlSymbol;

import java.sql.ResultSet;
import java.util.List;

/**
 * @Author: zhe
 * @Date: 2021/6/16 10:30
 * @Statement: 新闻类型Dao层接口实现类
 */
public class NewsTypeDaoImpl implements INewsTypeDao {

    //    JDBC连接工具类
    private static JdbcMysql jdbcMysql = JdbcMysql.getInstance();
    private static String sql = "";

    @Override
    public ResultSet findAll(Integer page, Integer limit, NewsType t) {
        sql = "select * from news_type where 1=1 ";
        if (page >= 0 && limit > 0) {
            sql += " limit "+page+SqlSymbol.COMMA.getValue()+limit;
        }
        ResultSet resultSet = jdbcMysql.jdbcQuery(sql);
        return resultSet;
    }

    @Override
    public ResultSet count(Integer page, Integer limit, NewsType t) {
        sql = "select count(1) from news_type where 1=1";
        if (page >= 0 && limit > 0) {
            sql += " limit "+page+SqlSymbol.COMMA.getValue()+limit;
        }
        ResultSet resultSet = jdbcMysql.jdbcQuery(sql);
        return resultSet;
    }

    @Override
    public ResultSet findById(Integer id) {
        sql = "select * from news_type where id = "+id;
        ResultSet resultSet = jdbcMysql.jdbcQuery(sql);
        return resultSet;
    }

    @Override
    public ResultSet findByParentId(List<Integer> ids) {
        sql = "select * from news_type where parent_id in (";
        if (ids.size() > 0) {
            for (int i = 0; i < ids.size(); i++) {
                if (i == ids.size() - 1) {
                    sql += ids.get(i)+SqlSymbol.RBRACKET.getValue();
                    break;
                }
                sql += ids.get(i)+SqlSymbol.COMMA.getValue();
            }
        }
        ResultSet resultSet = jdbcMysql.jdbcQuery(sql);
        return resultSet;
    }

    @Override
    public int save(NewsType t) {
        sql = "insert into news_type value " +
                "("+null+ SqlSymbol.COMMA.getValue()+SqlSymbol.MARK.getValue()+t.getName()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                t.getParentId()+SqlSymbol.COMMA.getValue()+
                SqlSymbol.MARK.getValue()+t.getGmtCreate()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                SqlSymbol.MARK.getValue()+t.getGmtModified()+SqlSymbol.MARK.getValue()+SqlSymbol.RBRACKET.getValue();
        int i = jdbcMysql.jdbcUpdate(sql);
        return i;
    }

    @Override
    public int update(NewsType t) {
        sql = "update news_type set " +
                "name = "+SqlSymbol.MARK.getValue()+t.getName()+SqlSymbol.MARK.getValue()+SqlSymbol.COMMA.getValue()+
                "gmt_modified = "+SqlSymbol.MARK.getValue()+t.getGmtModified()+SqlSymbol.MARK.getValue()+
                " where id = "+t.getId();
        int i = jdbcMysql.jdbcUpdate(sql);
        return i;
    }

    @Override
    public int updateParentId(NewsType t) {
        sql = "update news_type set " +
                "parent_id = "+t.getParentId()+
                "gmt_modified = "+SqlSymbol.MARK.getValue()+t.getGmtModified()+SqlSymbol.MARK.getValue()+
                " where id = "+t.getId();
        int i = jdbcMysql.jdbcUpdate(sql);
        return i;
    }

    @Override
    public int delete(Integer id) {
        sql = "delete from news_type where id = "+id;
        int i = jdbcMysql.jdbcUpdate(sql);
        return i;
    }

    @Override
    public int deleteIds(List<Integer> ids) {
        sql = "delete from news_type where id in (";
        if (ids.size() > 0) {
            for (int i = 0; i < ids.size(); i++) {
                if (i == ids.size() - 1) {
                    sql += ids.get(i)+SqlSymbol.RBRACKET.getValue();
                    break;
                }
                sql += ids.get(i)+SqlSymbol.COMMA.getValue();
            }
        }
        int i = jdbcMysql.jdbcUpdate(sql);
        return i;
    }


}
